package com.nashuju.web.controller.sys;

import java.util.ArrayList;
import java.util.List;

import org.durcframework.core.DefaultGridResult;
import org.durcframework.core.GridResult;
import org.durcframework.core.MessageResult;
import org.durcframework.core.controller.CrudController;
import org.durcframework.core.expression.Expression;
import org.durcframework.core.expression.ExpressionQuery;
import org.durcframework.core.expression.subexpression.ValueExpression;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nashuju.web.entity.sys.SysClass;
import com.nashuju.web.entity.sys.SysClassSch;
import com.nashuju.web.service.sys.SysClassService;

@Controller
public class SysClassController extends
		CrudController<SysClass, SysClassService> {

	@RequestMapping("/addCSysClass.do")
	public @ResponseBody MessageResult addCSysClass(SysClass entity) {
		return this.save(entity);
	}

	@RequestMapping("/listCSysClass.do")
	public @ResponseBody GridResult listCSysClass(SysClassSch searchEntity) {
		return this.query(searchEntity);
	}

	@RequestMapping("/updateCSysClass.do")
	public @ResponseBody MessageResult updateCSysClass(SysClass entity) {
		return this.update(entity);
	}

	@RequestMapping("/delCSysClass.do")
	public @ResponseBody Object delCSysClass(SysClass entity) {
		if (this.getService().hasChild(entity)) {
			return error(entity.getClassName() + "下含有子节点，不能删除！");
		}
		return this.delete(entity);
	}

	// 获取分类
	@RequestMapping("/listSysClass.do")
	public @ResponseBody Object listSysClass(SysClass entity) {
		ExpressionQuery query = ExpressionQuery.buildQueryAll();
		List<Expression> expressions = new ArrayList<Expression>();
		expressions.add(new ValueExpression("classtypeid", entity
				.getClasstypeid()));
		query.addAll(expressions);
		DefaultGridResult resultGrid = (DefaultGridResult) this.queryAll(query);
		@SuppressWarnings("unchecked")
		List<SysClass> rows = (List<SysClass>) resultGrid.getRows();
		return rows;
	}

	@RequestMapping("/getSysClassById.do")
	public @ResponseBody SysClass getSysClassById(SysClass entity) {
		return this.getService().get(entity.getScId());
	}

}
